package com.example.litterpainterdemo.draw.view

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.children

class ToolContainer(context: Context,attrs:AttributeSet?):ConstraintLayout(context, attrs) {
    private var lastToolView: ToolView? = null//记录上一次点击的工具
    var viewMOdel: DrawViewModel? = null

    //解析完毕
    override fun onFinishInflate() {
        super.onFinishInflate()
        children.forEach {
            if(it !is ImageView){
                val child = it as ToolView
                child.toolViewListener = {view,type->

                    if(lastToolView == null){
                            //选中
                            view.changeToolViewState(true)
                            lastToolView = view
                    }else{
                        if(lastToolView == view){
                            lastToolView!!.changeToolViewState(false)
                            lastToolView = null
                        }else {
                            lastToolView!!.changeToolViewState(false)
                            view.changeToolViewState(true)
                            lastToolView = view
                        }
                    }
                    //传递数据
                    viewMOdel!!.setToolType(type)
                }
            }
        }
    }
}